﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using WebService.DataAccess;using WebService.BusinessEntities;
using System.Data.SqlClient;
using System.Diagnostics;
using WebService.DataAccess.SQLServer;

namespace WebService.DataAccess
{
    /// <summary>
    /// Respository that lets you find LoginAudit in the
    /// CRM database.
    /// </summary>
    public class LoginAuditRepository : Repository<LoginAudit>
    {
        private string databaseName;

        public LoginAuditRepository(string databaseName)
            : base(databaseName)
        {
            this.databaseName = databaseName;
        }
        

		public List<LoginAudit> GetAllLoginAuditProc()
        {
            ISelectionFactory<NullableIdentity> selectionFactory = new GET_ALL_LOGIN_AUDIT_PROCSelectionFactory();
            
            try
            {
				NullableIdentity nullableIdentity = new NullableIdentity();
				return base.Find(selectionFactory, new GET_ALL_LOGIN_AUDIT_PROCFactory(), nullableIdentity);
			}
            catch (SqlException ex)
            {
                HandleSqlException(ex, selectionFactory);
            }
            
            return new List<LoginAudit>();
        }

        public List<LoginAudit> GetLoginAuditProc(System.Int32 id)
        {
            ISelectionFactory<System.Int32> selectionFactory = new GET_LOGIN_AUDIT_PROCSelectionFactory();
            
            try
            {
				return base.Find(selectionFactory, new GET_LOGIN_AUDIT_PROCFactory(), id);
			}
            catch (SqlException ex)
            {
                HandleSqlException(ex, selectionFactory);
            }
            
            return new List<LoginAudit>();
        }

        public void AddLogin(LoginAudit loginAudit)
        {
            LoginAuditInsertFactory insertFactory = new LoginAuditInsertFactory();
            try
            {
                base.Add(insertFactory, loginAudit);
            }
            catch (SqlException ex)
            {
                HandleSqlException(ex, insertFactory);
            }
        }

        public void AddLogout(LoginAudit loginAudit)
        {
            LogoutAuditInsertFactory insertFactory = new LogoutAuditInsertFactory();
            try
            {
                base.Add(insertFactory, loginAudit);
            }
            catch (SqlException ex)
            {
                HandleSqlException(ex, insertFactory);
            }
        }

		public void Save(LoginAudit loginAudit)
        {
            LoginAuditUpdateFactory updateFactory = new LoginAuditUpdateFactory();
            try
            {
                base.Save(updateFactory, loginAudit);
            }
            catch (SqlException ex)
            {
                HandleSqlException(ex, updateFactory);
            }
        }

        private void HandleSqlException(SqlException ex, IDbToBusinessEntityNameMapper mapper)
        {
            if(ex.Number == ErrorCodes.SqlUserRaisedError)
            {
                switch(ex.State)
                {
                    case ErrorCodes.ValidationError:
                        string[] messageParts = ex.Errors[0].Message.Split(':');
                        throw new RepositoryValidationException(
                            mapper.MapDbParameterToBusinessEntityProperty(messageParts[0]),
                            messageParts[1], ex);

                    case ErrorCodes.ConcurrencyViolationError:
                        throw new ConcurrencyViolationException(ex.Message, ex);

                }
            }

            throw new RepositoryFailureException(ex);
        }
    }
}

